Shader "Dan/2D/Uber2D"
{
	Properties
	{
		
		_MainTex("Sprite Texture",2D)="white"{}
		_Color("Tint",Color)=(1,1,1,1)
		[MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
        [HideInInspector] _RendererColor ("RendererColor", Color) = (1,1,1,1)
        [HideInInspector] _Flip ("Flip", Vector) = (1,1,1,1)
        [PerRendererData] _AlphaTex ("External Alpha", 2D) = "white" {}
        [PerRendererData] _EnableExternalAlpha ("Enable External Alpha", Float) = 0
        //default sprite properties

        //pixelate
        
        [Toggle(USE_PIXELATE)]_UsePixelate("Use Pixel Style",Int) =0
        _PixelResolution ("Pixel Resolution",Range(4,1024))=32

        //blur
        [Toggle(USE_BLUR)] _UseBlur("Use Blur", Int) = 0
        _BlurIntensity("Blur Intensity", Range(0,0.1) )= 0

        //desaturate
        [Toggle(USE_DESATURATE)] _UseDesaturate("Use Desaturate", Int) = 0
        _DesaturateFactor("Desaturate Factor", Range(0,1)) = 1 


        //shadow
        [Toggle(USE_SHADOW)] _UseShadow("Use Shadow", Int) = 0
        _ShadowColor("Shadow Color", Color) = (0, 0, 0, 1) 
        _ShadowOffset("ShadowOffsetXY_ScaleZW", Vector) = (0.1,0.1,0,0) 

        //chromaticAberrFactor
        [Toggle(USE_CHROMATIC_ABERRATION)] _UseChromaticAberr("Use Chromatic Aberration", Int) = 0
        _ChromaticAberrFactor("Chromatic Factor", Range(0, 1)) = 0
        _ChromaticAberrAlpha("Chromatic Alpha", Range(0, 1)) = 0.4 

        
        //outline
        [Toggle(USE_OUTLINE)] _UseOutline("Use Outline", Int) = 0
        _OutlineColor("Outline Base Color", Color) = (0,0,0,1) 
        _OutlineWidth_Alpha_Glow("OutlineWidth_Alpha_Glow",  Vector) = (0.001,1,1,0) 

        //innerOutline
        [Toggle(USE_INNER_OUTLINE)] _UseInnerOutline("Use InnerOutline", Int) = 0
        _InnerOutlineColor("Inner Outline Color", Color) = (0,0,0,1) 
        _InnerOutlineWidth_Alpha_Glow("InnerOutlineWidth_Alpha_Glow",  Vector) = (0.001,1,1,0)

        //oldphoto
        [Toggle(USE_OLDPHOTO)] _UseOldPhoto("Use OldPhoto", Int) = 0
        _OldPhotoProgress("Old Photo Progress",Range(0,1))=0.5

        //dissolve
        [Toggle(USE_DISSOLVE)] _UseDissolve("Use Dissolve", Int) = 0
        [NoScaleOffset]_DissolveTex("Dissolve Tex", 2D) ="white"{}
        _DissolveProgress("Dissolve Progress",Range(-1,1))=0
        [KeywordEnum(Off,X,Y)]
        _Dir("Dissolve Dir",Float)=0
        [HDR]_DissolveColor("Dissolve Color",Color)=(1,1,1,1)
        _DissolveRage("Dissolve Rage",Range(0,1))=0.1

        //distort
        [Toggle(USE_DISTORT)]_UseDistort("Use Distort",Int)=0
       	[NoScaleOffset]_DistortTex("Distort Tex", 2D) ="white"{}
       	_DistortStrength("Distort Strength",Range(0,1))=0
       	_WaveX("Wave X",Range(0,1))=0
        _WaveY("Wave Y",Range(0,1))=0
        _DistanceX("DistanceX",Range(0,1))=0
        _DistanceY("DistanceY",Range(0,1))=0
        _Speed("Distort Speed",Float)=0

	}

	SubShader 
	{
		Tags
		{
			"Queue" ="Transparent"
			"IgnoreProjector"="True"
			"RenderType"="Transparent"
			"PreviewType"="Plane"
			"CanUseSpriteAtlas" ="True"
		}
		
		Cull  Off
		Lighting Off
		ZWrite  Off
		Blend  One OneMinusSrcAlpha

		Pass 
		{
		CGPROGRAM
			#pragma vertex SpriteVert
            #pragma fragment Frag
            #pragma target 2.0
            #pragma multi_compile_instancing
            #pragma multi_compile _ PIXELSNAP_ON
            #pragma multi_compile _ ETC1_EXTERNAL_ALPHA

			#include "UnitySprites.cginc"
			#include "../ShaderLibs/PostProcess.cginc"
			
			#pragma shader_feature USE_PIXELATE 
			#pragma shader_feature USE_BLUR
			#pragma shader_feature USE_DESATURATE
            #pragma shader_feature USE_SHADOW
            #pragma shader_feature USE_CHROMATIC_ABERRATION
            #pragma shader_feature USE_OUTLINE
            #pragma shader_feature USE_INNER_OUTLINE
			#pragma shader_feature USE_OLDPHOTO
			#pragma shader_feature USE_DISSOLVE
			#pragma shader_feature USE_DISTORT
			#pragma shader_feature _DIR_OFF  _DIR_X  _DIR_Y

		#ifdef USE_PIXELATE
			fixed _PixelResolution;
		#endif //USE_PIXELATE			

		#ifdef USE_BLUR
            fixed _BlurIntensity;
        #endif//USE_BLUR	

        #ifdef USE_DESATURATE
            fixed _DesaturateFactor;
        #endif//USE_DESATURATE

        #ifdef USE_SHADOW
            fixed4 _ShadowOffset;
            fixed _ShadowAlpha;
            fixed4 _ShadowColor;
        #endif//USE_SHADOW

        #ifdef USE_CHROMATIC_ABERRATION
            fixed _ChromaticAberrFactor;
            fixed _ChromaticAberrAlpha;
        #endif//USE_CHROMATIC_ABERRATION

        #if USE_OUTLINE
            fixed4 _OutlineColor;
            fixed4 _OutlineWidth_Alpha_Glow;
        #endif//USE_OUTLINE
        
        #if USE_INNER_OUTLINE
            fixed4 _InnerOutlineColor;
            fixed4 _InnerOutlineWidth_Alpha_Glow;
        #endif//USE_INNER_OUTLINE
        
        #if  USE_OLDPHOTO  
        	fixed _OldPhotoProgress;    
        #endif//USE_OLDPHOTO 
		
        #if USE_DISSOLVE
        	sampler2D _DissolveTex;
        	fixed  _DissolveProgress;
        	fixed4 _DissolveColor;
        	fixed _Dir;
        	fixed _DissolveRage;	
        #endif//USE_DISSOLVE

		#if USE_DISTORT
        	sampler2D _DistortTex;
        	fixed _DistortStrength;
        	fixed _WaveX;
        	fixed _WaveY;
        	fixed _DistanceX;
        	fixed _DistanceY;
        	fixed _Speed;
		#endif//USE_DISTORT

			void UVOperate(inout float2 uv){
				float2 rawUV = uv;
            #ifdef USE_PIXELATE
				uv = Pixelate(uv,_PixelResolution);
            #endif//USE_PIXELATE
				
			#ifdef USE_DISTORT	
				float2 disuv = Distort2D(rawUV,_WaveX,_WaveY,_DistanceX,_DistanceY,_Speed);
				float dis =tex2D(_DistortTex,disuv);
				dis =(dis*2-1)*_DistortStrength;
				uv+=dis;
			#endif//USE_DISTORT


			}

			void ColorOperate(float2 rawUV,float2 uv,float4 inColor,inout float4 col){

			#ifdef USE_DISSOLVE
				#if _DIR_OFF
                	_Dir=0; 	
    			#elif _DIR_X
                	_Dir=uv.x;
    			#elif _DIR_Y
                	_Dir=uv.y;
				#endif
                 col =Dissolve2D(uv,_MainTex,_DissolveTex,_Dir,_DissolveProgress,_DissolveRage,_DissolveColor);
			#endif//USE_DISSOLVE		    

			#ifdef USE_BLUR
                col = Blur(_MainTex,rawUV,_BlurIntensity);
            #endif//USE_BLUR

            

            #ifdef USE_CHROMATIC_ABERRATION
                col = ChromaticAberration(col,_MainTex, rawUV,inColor * _Color,_ChromaticAberrFactor,_ChromaticAberrAlpha);
            #endif//USE_CHROMATIC_ABERRATION

            #ifdef USE_DESATURATE
                col = Desaturate(col, _DesaturateFactor);
            #endif//USE_DESATURATE

            #if USE_INNER_OUTLINE
                col = DrawInnerOutline (col,_MainTex, rawUV,_InnerOutlineColor, _InnerOutlineWidth_Alpha_Glow.x, _InnerOutlineWidth_Alpha_Glow.y, _InnerOutlineWidth_Alpha_Glow.z);
            #endif//USE_INNER_OUTLINE

            #ifdef USE_OUTLINE
                col = DrawOutline (col,_MainTex, rawUV,_OutlineColor, _OutlineWidth_Alpha_Glow.x, _OutlineWidth_Alpha_Glow.y, _OutlineWidth_Alpha_Glow.z);
            #endif//USE_OUTLINE

            #ifdef USE_OLDPHOTO
                col = OldPhoto (col,_OldPhotoProgress);
            #endif//USE_OLDPHOTO

            #ifdef USE_SHADOW 
                rawUV = float2(uv.x/_ShadowOffset.z,uv.y/_ShadowOffset.w);
                col = Shadow2D(col,_MainTex,rawUV,_ShadowOffset,_ShadowColor);  
            #endif//USE_SHADOW
            
               

			}	


			fixed4 Frag(v2f IN) : SV_Target
			{
				float2 uv = IN.texcoord;
				float2 rawUV = uv;
				
				UVOperate(uv);
			
				
				fixed4 col = SampleSpriteTexture (uv) * IN.color;
				

				
				ColorOperate(rawUV,uv,IN.color,col);

				col.rgb *= col.a;
				return col;
            }//frag	

		ENDCG
		}//Pass   

	}//SubShader
	CustomEditor "Dan.Uber2DGUI"
}